package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.UserBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.constants.KafkaConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
public class UserBehaviorServiceImpl implements UserBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {

        //1.检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.是否登录
        Integer userID = AppThreadLocalUtil.getUserID();
        if (userID == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }


        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        //判断是否已经点赞
        if(cacheService.get(userID + dto.getArticleId() + BehaviorConstants.LIKE_BEHAVIOR) != null){
            //已点赞,取消点赞
            cacheService.delete(userID + dto.getArticleId() + BehaviorConstants.LIKE_BEHAVIOR);
            //向article服务发出异步请求减少点赞数量
//            kafkaTemplate.send(KafkaConstants.AP_ARTICLE_LIKE_ROLLBACK,dto.getArticleId().toString());
            mess.setAdd(-1);
        }else {
            //未点赞,进行点赞操作
            cacheService.set(userID + dto.getArticleId() + BehaviorConstants.LIKE_BEHAVIOR,userID.toString());
            //向article服务发出异步请求增加点赞数量
//            kafkaTemplate.send(KafkaConstants.AP_ARTICLE_LIKE,dto.getArticleId().toString());
            mess.setAdd(1);
        }
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        //1.检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.是否登录
        Integer userID = AppThreadLocalUtil.getUserID();
        if (userID == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //判断是否已经点不喜欢
        if(cacheService.get(userID + dto.getArticleId() + BehaviorConstants.UN_LIKE_BEHAVIOR) != null){
            //已点,取消操作
            cacheService.delete(userID + dto.getArticleId() + BehaviorConstants.UN_LIKE_BEHAVIOR);
        }else {
            //未点,进行操作
            cacheService.set(userID + dto.getArticleId() + BehaviorConstants.LIKE_BEHAVIOR,userID.toString());
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        //1.检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.是否登录
        Integer userID = AppThreadLocalUtil.getUserID();
        if (userID == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

//        UpdateArticleMess mess = new UpdateArticleMess();
//        mess.setArticleId(dto.getArticleId());
//        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        //更新阅读次数
        kafkaTemplate.send(KafkaConstants.AP_ARTICLE_READ,dto.getArticleId().toString());
//        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));
//        String readBehaviorJson = (String) cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
//        if (StringUtils.isNotBlank(readBehaviorJson)) {
//            ReadBehaviorDto readBehaviorDto = JSON.parseObject(readBehaviorJson, ReadBehaviorDto.class);
//            dto.setCount((short) (readBehaviorDto.getCount() + dto.getCount()));
//        }
//        // 保存当前key
//        log.info("保存当前key:{} {} {}", dto.getArticleId(), user.getId(), dto);
//        cacheService.hPut(BehaviorConstants.READ_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));

        return null;

    }
}